

#include "SnapGenerator.h"
#include "Setting.h"

#include <stdio.h>
#define UPDATE_TIME 10000   //ms

SnapGenerator::SnapGenerator(SpeedMonitor* speedMonitor, Setting* setting) : m_setting(setting)
{
	m_accum = 0;
	m_speedMonitor = speedMonitor;
	m_timer = new QTimer(this);

	connect(m_timer, SIGNAL(timeout()), this, SLOT(updateAccumulator()));
	m_timer->start(UPDATE_TIME);
}



SnapGenerator::~SnapGenerator()
{
	m_timer->stop();
	delete m_timer;
}

void SnapGenerator::cancelWarnning()
{
	m_accum = 0;
	m_timer->start(UPDATE_TIME);
}

void SnapGenerator::carHappen()
{
	m_timer->stop();
	m_accum = 0;
}

void SnapGenerator::updateAccumulator()
{
	int len = m_speedMonitor->speed() * UPDATE_TIME / 1000;
	printf("this time len = %d\n", len);
	m_accum += len;
	printf("m_accum = %d\n", m_accum);

	if(m_accum > m_setting->pipeLen())
	{
		m_accum = 0;
		emit snapCaculSignal();
	}
	else
	{
	}
}
